<?php

namespace App\Http\Controllers\web;

use App\Http\Controllers\Controller;
use App\Models\users\UsersBankModel;
use App\Service\users\UsersSaveService;
use App\Service\users\UsersService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use lxs\api\ApiResponse;
use lxs\tools\Tools;

class UsersController extends Controller
{

    /**
     * 获取用户信息
     */
    public function getUserInfoData(Request $request): JsonResponse
    {
        $userInfo = UsersService::instance()->getUserInfo($request->web_uid);
        // 小程序二维码
        $userInfo['xcx_qrcode'] = "https://ouan-retail.oss-cn-shenzhen.aliyuncs.com/tmui/avatar/2407041201028947.jpg";
        // 客服二维码
        $userInfo['service_qrcode'] = "https://ouan-retail.oss-cn-shenzhen.aliyuncs.com/web/other/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20240826162440.png";

        return ApiResponse::apiResponseData(200, '获取用户信息成功', [
            'user_info' => $userInfo,
        ]);
    }


    /**
     * 获取用户详细信息
     */
    public function getUserDetailInfo(Request $request): JsonResponse
    {
        $type = $request->input('type');

        $userInfo = UsersService::instance()->getUserDetail($request->web_uid);
        $userInfo->withdraw_commission = Tools::Config('USER.withdraw_commission');

        if (isset($type) && $type === 'withdraw') {
            $userInfo->userAlipay->icon = "https://ouan-retail.oss-cn-shenzhen.aliyuncs.com/web/finance/%E6%94%AF%E4%BB%98%E5%AE%9D.png";
            $userInfo->userAlipay->payee_account = Tools::hideNumber($userInfo->userAlipay->payee_account);

            $userInfo->userBank->icon = UsersBankModel::getIcon($userInfo->userBank->bank_name);
            $userInfo->userBank->bank_number = Tools::hideNumber($userInfo->userBank->bank_number, 'bank');
            $userInfo->userBank->bank_name = mb_substr($userInfo->userBank->bank_name, 0, 4);
        }

        return ApiResponse::apiResponseData(200, '获取用户详细信息成功', [
            'user_info' => $userInfo,
        ]);
    }


    /**
     * 实名认证表单提交
     */
    public function userAuthenticationSubmit(Request $request): JsonResponse
    {
        $uid = $request->web_uid;
        $formData = $request->input();

        $result = UsersSaveService::instance()->saveRealName($uid, $formData);
        if ($result['code'] == 200) {
            return ApiResponse::success($result['message']);
        }
        return ApiResponse::error($result['message']);
    }


    /**
     * 获取收款账户页面数据
     */
    public function getAccountNumberData(Request $request): JsonResponse
    {
        $userInfo = UsersService::instance()->getUserDetail($request->web_uid);

        return ApiResponse::apiResponseData(200, '获取收款账户页面数据成功', [
            'user_info' => $userInfo,
        ]);
    }


    /**
     * 收款账户表单提交
     */
    public function paymentAccountSubmit(Request $request): JsonResponse
    {
        $post = $request->post();
        $uid = $request->web_uid;

        if ( isset($post['payeeType']) && $post['payeeType'] == 1 ) { // 支付宝

            if ( $post['user_alipay']['payee_account'] == '' ) {
                return ApiResponse::error('请填写收款账号');
            }
            if ( $post['user_alipay']['payee_name'] == '' ) {
                return ApiResponse::error('请填写收款名称');
            }

            $result = UsersSaveService::instance()->saveAlipayAccount($uid, $post['user_alipay']);

            return ApiResponse::success($result['message']);

        } else if ( isset($post['payeeType']) && $post['payeeType'] == 2 ) { // 银行卡

            if ( $post['user_bank']['bank_name'] == '' ) {
                return ApiResponse::error('请输入银行名称');
            }
            if ( $post['user_bank']['bank_info'] == '' ) {
                return ApiResponse::error('请输入开户支行');
            }
            if ( $post['user_bank']['payee'] == '' ) {
                return ApiResponse::error('请输入收款名称');
            }
            if ( $post['user_bank']['bank_number'] == '' ) {
                return ApiResponse::error('请输入收款账户');
            }

            $result = UsersSaveService::instance()->saveBankCard($uid, $post['user_bank']);

            return ApiResponse::success($result['message']);
        }

        return ApiResponse::error('类型错误！');
    }


    /**
     * 注销账号表单提交
     */
    public function cancelAccountSubmit(Request $request): JsonResponse
    {
        $uid = $request->web_uid;
        $result = UsersSaveService::instance()->saveCancelAccount($uid);
        if ($result['code'] == 200) {
            return ApiResponse::success($result['message']);
        }
        return ApiResponse::error($result['message']);
    }


}
